package com.serviceprovider.business.module.admin.settings.services;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.settings.model.DefaultFee;
import com.serviceprovider.business.module.admin.settings.model.PropertyName;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Service;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Date;
import java.util.List;

/**
 * Implementation of interface IPropertiesService
 *
 * @author mrduongnv@gmail.com
 */
@Service("settingsService")
public class SettingsService extends SimpleJdbcDaoSupport implements ISettingsService {
    @Autowired
    public SettingsService(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    public Integer getDefaultFee(Date effectiveFrom) {
        return getSimpleJdbcTemplate().queryForInt("SELECT default_fee " + "FROM default_sp_fees "
                + "WHERE effective_from > ? " + "AND effective_from < ? " + "ORDER BY effective_from DESC "
                + "LIMIT 1", effectiveFrom, new Date());
    }

    public Integer getDefaultFee() {
        return getDefaultFee(new Date(0));
    }

    @Override
    public String getUiTheme() {
        return getSimpleJdbcTemplate().queryForObject("SELECT value " + "FROM site_settings " + "WHERE property = ? ",
                String.class, PropertyName.UI_THEME);
    }

    @Override
    public List<DefaultFee> getDefaultFees(Date effectiveFrom) {
        if (effectiveFrom == null) {
            effectiveFrom = new Date(0);
        }

        return getSimpleJdbcTemplate().query("SELECT * " + "FROM default_sp_fees " + "WHERE effective_from > ? "
                + "ORDER BY effective_from DESC ", new DefaultFeeMapper(), effectiveFrom);
    }

    @Override
    public void deleteDefaultFee(Integer feeId) {

        // Do not allow deleting default SP fee from prehistoric times
        if (feeId == 1) {
            return;
        }

        getSimpleJdbcTemplate().update("DELETE FROM default_sp_fees WHERE id = ?", feeId);
    }

    @Override
    public void createDefaultFee(DefaultFee fee) {
        getSimpleJdbcTemplate().update("INSERT INTO default_sp_fees (default_fee, effective_from) " + "VALUES (?, ?)",
                                       fee.getFee(), fee.getEffectiveFrom());
    }

    @Override
    public void updateUiTheme(String uiTheme) {
        updateSetting(PropertyName.UI_THEME, uiTheme);
    }

    @Override
    public Integer getDefaultCheckPrintedBy() {
        return getIntegerSetting(PropertyName.CHECK_PRINT_BY);
    }

    @Override
    public Integer getDefaultOrderApproval() {
        return getIntegerSetting(PropertyName.ORDER_APPROVAL);
    }

    @Override
    public void setDefaultCheckPrintedBy(Integer printedBy) {
        updateSetting(PropertyName.CHECK_PRINT_BY, printedBy.toString());
    }

    @Override
    public void setDefaultOrderApproval(Integer orderApproval) {
        updateSetting(PropertyName.ORDER_APPROVAL, orderApproval.toString());
    }

    private Integer getIntegerSetting(String propertyName) {
        return getSimpleJdbcTemplate().queryForInt("SELECT value " + "FROM site_settings " + "WHERE property = ? ",
                propertyName);
    }

    private void updateSetting(String propertyName, String value) {
        getSimpleJdbcTemplate().update("UPDATE site_settings " + "SET value = ? " + "WHERE property = ?", value,
                                       propertyName);
    }

    private class DefaultFeeMapper implements ParameterizedRowMapper {
        @Override
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            DefaultFee fee = new DefaultFee();

            fee.setId(rs.getInt("id"));
            fee.setFee(rs.getInt("default_fee"));
            fee.setEffectiveFrom(rs.getDate("effective_from"));

            return fee;
        }
    }
}



